﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Waiter.Common;
using Waiter.Controler;
using Waiter.DataLayer;

namespace Waiter.View
{
    public partial class FrmRptBill : DevComponents.DotNetBar.Office2007Form
    {
        public FrmRptBill()
        {
            InitializeComponent();
        }

        private void btnClose_Click(object sender, EventArgs e)
        {
            CommonLogging.fsWriteLog(CommonConstants.LOG_TYPE_INFO, "FrmRptBill.btnClose_Click", "", "");
            this.Close();
        }

        private void FrmRptBill_Load(object sender, EventArgs e)
        {
            CommonLogging.fsWriteLog(CommonConstants.LOG_TYPE_INFO, "FrmRptBill.FrmRptBill_Load", "", "");
            DateTime currentDate = DateTime.Now;
            this.dtiDay.Value = currentDate;
            this.dtiMonth.Value = currentDate;
            this.dtiTo.Value = currentDate;
            this.dtiFrom.Value = currentDate;
            this.chkHourWork.Checked = true;
            this.chkAll.Checked = true;
            this.fsCheckChange(null, null);
            fsLoadCMBHowWork();
        }
        private void fsLoadCMBHowWork()
        {
            CommonLogging.fsWriteLog(CommonConstants.LOG_TYPE_INFO, "FrmRptBill.fsLoadCMBHowWork", "", "");
            WorkShiftCtrl workShiftCtrl = new WorkShiftCtrl();
            List<WORK_SHIFT> workShiftList = workShiftCtrl.fsGetWorkShiftList();
            List<ComboboxItem> itemList = new List<ComboboxItem>();
            if (workShiftList == null || workShiftList.Count == 0)
            {
                return;
            }

            for (int i = 0; i < workShiftList.Count; i++)
            {
                ComboboxItem item = new ComboboxItem();
                item.Value = workShiftList[i].CODE;
                item.Text = workShiftList[i].WORK_SHIFT_NAME;

                itemList.Add(item);
            }

            this.cmbHourWork.DataSource = itemList;
            this.cmbHourWork.DisplayMember = CommonConstants.CMB_TEXT;
            this.cmbHourWork.ValueMember = CommonConstants.CMB_VALUE;
            CommonCtrl.fsCmbSetSelectedIndex(this.cmbHourWork, 0);
        }
        private void fsCheckChange(object sender, EventArgs e)
        {
            CommonLogging.fsWriteLog(CommonConstants.LOG_TYPE_INFO, "FrmRptBill.fsCheckChange", "", "");
            this.cmbHourWork.Enabled = this.chkHourWork.Checked;
            this.dtiDay.Enabled = this.chkDay.Checked;
            this.dtiMonth.Enabled = this.chkMonth.Checked;
            this.dtiFrom.Enabled = this.chkFromTo.Checked;
            this.dtiTo.Enabled = this.chkFromTo.Checked;
        }

        private void btnSearch_Click(object sender, EventArgs e)
        {
            CommonLogging.fsWriteLog(CommonConstants.LOG_TYPE_INFO, "FrmRptBill.btnSearch_Click", "", "");
            if (!this.fsCheckDateFromTo(this.dtiFrom))
            {
                return;
            }

            DateTime startDate = DateTime.Now;
            DateTime endDate = DateTime.Now;
            WorkShiftCtrl workShiftCtrl = new WorkShiftCtrl();
            MasterCtrl masterCtrl = new MasterCtrl();

            if (this.chkHourWork.Checked)
            {
                WORK_SHIFT item = workShiftCtrl.fsFindByCode(CommonCtrl.fsCmbGetSelectedValue(this.cmbHourWork));
                if (item != null)
                {
                    startDate = new DateTime(startDate.Year, startDate.Month, startDate.Day, item.START_TIME.Hours, item.START_TIME.Minutes, item.START_TIME.Seconds);
                    endDate = new DateTime(endDate.Year, endDate.Month, endDate.Day, item.END_TIME.Hours, item.END_TIME.Minutes, item.END_TIME.Seconds);
                }
            }
            else if (this.chkDay.Checked)
            {
                startDate = new DateTime(startDate.Year, startDate.Month, startDate.Day);
                endDate = new DateTime(endDate.Year, endDate.Month, endDate.Day, 23, 59, 59);
            }
            else if (this.chkMonth.Checked)
            {
                startDate = new DateTime(this.dtiMonth.Value.Year, this.dtiMonth.Value.Month, 1);
                endDate = new DateTime(this.dtiMonth.Value.Year, this.dtiMonth.Value.Month, DateTime.DaysInMonth(this.dtiMonth.Value.Year, this.dtiMonth.Value.Month), 23, 59, 59);
            }
            else if (this.chkFromTo.Checked)
            {
                startDate = new DateTime(this.dtiFrom.Value.Year, this.dtiFrom.Value.Month, this.dtiFrom.Value.Day);
                endDate = new DateTime(this.dtiTo.Value.Year, this.dtiTo.Value.Month, this.dtiTo.Value.Day, 23, 59, 59);
            }

            int billType = 1;
            if (this.chkAll.Checked)
            {
                billType = 1;
            }
            else if (this.chkUse.Checked)
            {
                billType = 2;
            }
            else if (this.chkNotUse.Checked)
            {
                billType = 3;
            }

            this.drgBill.Rows.Clear();
            ReportCtrl reportCtrl = new ReportCtrl();
            List<ViewRptBill> resultList = reportCtrl.fsGetTotalBillList(startDate, endDate, billType);
            if (resultList == null || resultList.Count == 0)
            {
                DevComponents.DotNetBar.MessageBoxEx.Show("Không tìm thấy kết quả thống kê!", CommonMessages.MSG_NOTICE_TITLE, MessageBoxButtons.OK, MessageBoxIcon.Information);
                return;
            }

            this.drgBill.Rows.Add(resultList.Count);
            for (int i = 0; i < resultList.Count; i++)
            {
                this.drgBill.Rows[i].Cells["colSTT"].Value = i + 1;
                this.drgBill.Rows[i].Cells["colCode"].Value = resultList[i].code;
                this.drgBill.Rows[i].Cells["colCreateDate"].Value = resultList[i].createDate;
                this.drgBill.Rows[i].Cells["colStaff"].Value = CommonUtils.fsGetOPName(resultList[i].staff);
                this.drgBill.Rows[i].Cells["colTable"].Value = resultList[i].tableName;
                this.drgBill.Rows[i].Cells["colTotal"].Value = CommonUtils.fsCreateMoneyString(resultList[i].total);
                this.drgBill.Rows[i].Cells["colNote"].Value = resultList[i].note;
                this.drgBill.Rows[i].Cells["colStatus"].Value = resultList[i].status;
                if (resultList[i].status == (int)BillStatus.NOT_USE)
                {
                    this.drgBill.Rows[i].DefaultCellStyle.BackColor = Color.Gray;
                }
            }
        }

        private bool fsCheckDateFromTo(Control focusItem)
        {
            CommonLogging.fsWriteLog(CommonConstants.LOG_TYPE_INFO, "FrmRptBill.fsCheckDateFromTo", "", "");
            try
            {
                if (this.dtiFrom.Value > this.dtiTo.Value)
                {
                    DevComponents.DotNetBar.MessageBoxEx.Show("Ngày không hợp lệ!", CommonMessages.MSG_ERR_TITLE, MessageBoxButtons.OK, MessageBoxIcon.Error);
                    focusItem.Focus();
                    return false;
                }
            }
            catch (Exception ex)
            {
                CommonLogging.fsWriteLog(CommonConstants.LOG_TYPE_INFO, "FrmRptBill.fsCheckDateFromTo", "", ex.ToString());
            }
            return true;
        }

        private void dtiFrom_ValueChanged(object sender, EventArgs e)
        {
            CommonLogging.fsWriteLog(CommonConstants.LOG_TYPE_INFO, "FrmRptBill.dtiFrom_ValueChanged", "", "");
            fsCheckDateFromTo(this.dtiFrom);
        }

        private void dtiTo_ValueChanged(object sender, EventArgs e)
        {
            CommonLogging.fsWriteLog(CommonConstants.LOG_TYPE_INFO, "FrmRptBill.dtiTo_ValueChanged", "", "");
            fsCheckDateFromTo(this.dtiTo);
        }

        private void btnDetail_Click(object sender, EventArgs e)
        {
            CommonLogging.fsWriteLog(CommonConstants.LOG_TYPE_INFO, "FrmRptBill.btnDetail_Click", "", "");
            if (this.drgBill.Rows.Count == 0)
            {
                return;
            }

            string billCode = this.drgBill.CurrentRow.Cells["colCode"].Value.ToString();
            frmBillDetail frm = new frmBillDetail();
            frm.fsSetBillCode(billCode);
            frm.ShowDialog();
        }

        private void btnDelete_Click(object sender, EventArgs e)
        {
            CommonLogging.fsWriteLog(CommonConstants.LOG_TYPE_INFO, "FrmRptBill.btnDelete_Click", "", "");
            if (this.drgBill.Rows.Count == 0)
            {
                return;
            }

            int status = Convert.ToInt32(this.drgBill.CurrentRow.Cells["colStatus"].Value);
            if (status == (int)BillStatus.NOT_USE)
            {
                return;
            }


            BillCtrl billCtrl = new BillCtrl();
            FrmInputText frmInput = new FrmInputText();
            frmInput.fsSetDialogInfo("Lý do hủy hóa đơn", true);
            if (frmInput.ShowDialog() != DialogResult.OK)
            {
                return;
            }
            string note = frmInput.fsGetInputText();
            string code = this.drgBill.CurrentRow.Cells["colCode"].Value.ToString();

            if (billCtrl.fsUpdateStatus(code, (int)BillStatus.NOT_USE, note) != ResultStatus.SUCCESS)
            {
                DevComponents.DotNetBar.MessageBoxEx.Show("Đã có lổi xảy ra, vui lòng thực hiện lại!", CommonMessages.MSG_ERR_TITLE, MessageBoxButtons.OK, MessageBoxIcon.Error);
                return;
            }

            this.drgBill.CurrentRow.DefaultCellStyle.BackColor = Color.Gray;
            this.drgBill.CurrentRow.Cells["colNote"].Value = note;
            this.drgBill.CurrentRow.Cells["colStatus"].Value = (int)BillStatus.NOT_USE;
        }

        private void btnReAdd_Click(object sender, EventArgs e)
        {
            CommonLogging.fsWriteLog(CommonConstants.LOG_TYPE_INFO, "FrmRptBill.btnReAdd_Click", "", "");
            if (this.drgBill.Rows.Count == 0)
            {
                return;
            }

            int status = Convert.ToInt32(this.drgBill.CurrentRow.Cells["colStatus"].Value);
            if (status == (int)BillStatus.USE)
            {
                return;
            }


            BillCtrl billCtrl = new BillCtrl();
            FrmInputText frmInput = new FrmInputText();
            frmInput.fsSetDialogInfo("Lý do lấy lại hóa đơn đã bị hủy", true);
            if (frmInput.ShowDialog() != DialogResult.OK)
            {
                return;
            }
            string note = frmInput.fsGetInputText();
            string code = this.drgBill.CurrentRow.Cells["colCode"].Value.ToString();

            if (billCtrl.fsUpdateStatus(code, (int)BillStatus.USE, note) != ResultStatus.SUCCESS)
            {
                DevComponents.DotNetBar.MessageBoxEx.Show("Đã có lổi xảy ra, vui lòng thực hiện lại!", CommonMessages.MSG_ERR_TITLE, MessageBoxButtons.OK, MessageBoxIcon.Error);
                return;
            }

            this.drgBill.CurrentRow.DefaultCellStyle.BackColor = System.Drawing.SystemColors.Window;
            this.drgBill.CurrentRow.Cells["colNote"].Value = note;
            this.drgBill.CurrentRow.Cells["colStatus"].Value = (int)BillStatus.USE;
        }

        private void btnEdit_Click(object sender, EventArgs e)
        {
            if (this.drgBill.Rows.Count == 0)
            { return; }

            string code = this.drgBill.CurrentRow.Cells["colCode"].Value.ToString();
            frmEditBill frmEditBill = new frmEditBill();
            frmEditBill.fsInputData(code);
            frmEditBill.ShowDialog();
            if (frmEditBill.DialogResult == System.Windows.Forms.DialogResult.OK)
            {
                BILL item = new BillCtrl().fsFindBillBycode(code);
                this.drgBill.CurrentRow.Cells["colStaff"].Value = CommonUtils.fsGetOPName(item.OPID);
                this.drgBill.CurrentRow.Cells["colNote"].Value = item.NOTE;
            }
        }
    }
}
